﻿/** 
 * Description:用户基本信息DAO
 * Date:2011-7-12
 * Author:Along	
*/

package com.community.shopping.dao;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.community.shopping.domain.User;
import com.community.shopping.service.logic.Page;



public class UserDao extends HibernateDaoSupport{
	private Log log = LogFactory.getLog(UserDao.class);  
	

	public void save(User user){
		getHibernateTemplate().saveOrUpdate(user);	
		log.debug("-------保存一行User-------");
	}

	public void delete(User user){		
		getHibernateTemplate().delete(user);
		log.debug("---------删除一行User-------");
	}


	public List<?> getUsers() {  		
	 return getHibernateTemplate().find(" from User"); 
	 }

	public List<?> getUsers(String sql){
		String finalsql="from User where "+sql;
		return getHibernateTemplate().find(finalsql);
	}   

	public List<?> getUsers(String properties,String operate,String value){
		String sql="from User where "+properties+""+operate+""+value;
		return getHibernateTemplate().find(sql);
	}

	public User getUsersByUserName(String userName){
		String finalsql=" from User where userName ='"+userName+"'";
		if(!getHibernateTemplate().find(finalsql).isEmpty())
		{
			return (User)getHibernateTemplate().find(finalsql).get(0);
		}
		return null;
	} 
	
	public User getUsersByID(String userid){
		String finalsql=" from User where id ='"+userid+"'";
		if(!getHibernateTemplate().find(finalsql).isEmpty())
		{
			return (User)getHibernateTemplate().find(finalsql).get(0);
		}
		return null;
	}
	
	public List<?> getUsers(String hql,Page page){
		String finalsql=" from User where "+hql;
		Query   query   = this.getSession().createQuery(finalsql+" order by createDate desc"); 
		query.setFirstResult(page.getPageStartRow()>0?page.getPageStartRow()-1:0); 
		query.setMaxResults(page.getPageSize()); 
		if(page.getTotalRowsAmount()==0)
		{
		page.setTotalRowsAmount(getTotalCount(finalsql));
		}
		List   stu   =   query.list(); 
		return stu;
	} 
	
	public int getTotalCount(String sql)
	{
		Query   query   = this.getSession().createQuery("select count(*) "+sql); 
		int i=Integer.parseInt(query.list().get(0).toString());
		return i;
	}


}